# -*- encoding:utf-8 -*-
from dao.MySqlDAL import MySqlDAL

__author__ = 'shudong.msd'

# table_name_news = 'tb_stock_news'
# table_name_debate = 'tb_stock_debate'


def filt_repeat(tablename):
    sql_util = MySqlDAL()
    query_repeat = 'select id from ' + tablename + ' where title in \
    (select title from ' + tablename + ' group by title having count(title)>1) group by title'

    num = 0
    while True:
        ids = sql_util.get_dimensions_rows(query_repeat)
        if len(ids) == 0:
            break
        del_repeat = 'delete from ' + tablename + ' where id in ('
        for row in ids:
            del_repeat += str(row['id']) + ','
        del_repeat = del_repeat[:len(del_repeat) - 1] + ')'
        num += sql_util.execute_sql(del_repeat)
    print '清除了'+tablename+'中'+str(num)+'条重复的记录'
    return num
